/**
 * 描述: 系统接口服务
 * 版权: Copyright (c) 2018
 * 公司: 深圳市网商天下科技开发有限公司
 * 作者: 陈元
 * 版本: 1.0.0
 * 创建时间: 2018/12/20 16:27
 */
import {Service, RequestParams} from 'vue-element-simple';
import {setToken, removeToken, getToken, removeSaaSId, setSaaSId, getSaaSId} from '../utils/auth'
import store from '../store'

Service.getToken = function () {
  return getToken();
};

/**
 * 用户登陆
 * @param params
 * @returns {Promise.<*>}
 */
Service.adminRoleLogin = async function (params = {}) {
  const request = new RequestParams();
  // 帐号名称
  request.put('username', params['username']).checkEmpty('帐号名称');
  // 帐号密码
  request.put('password', params['password']).checkEmpty('帐号密码');
  const result = await this.getService(`/admin/role/login`, request);
  if (result.code === 0) {
    const {token} = result.data || {};
    setToken(token);
    store.commit('SET_TOKEN', token);
    setSaaSId(result.data.mall.id)
  }
  return result;
};

/**
 * 链接跳转自动登陆
 * @param params
 * @returns {Promise.<*>}
 */
Service.adminAutoLogin = async function (params = {}) {
  const request = new RequestParams();
  request.put('wsToken', params['wsToken']);
  request.put('wsDomain', params['wsDomain']);
  const result = await this.getService(`/admin/role/login`, request);
  if (result.code === 0) {
    const {token} = result.data || {};
    setToken(token);
    store.commit('SET_TOKEN', token);
  }
  return result;
};

// 管理员用户信息
Service.ADMIN_USER_INFO = null;

/**
 * 获取管理员信息
 * @returns {Promise.<*>}
 */
Service.adminRoleGetAdminInfo = async function () {
  const request = new RequestParams();
  if (this.ADMIN_USER_INFO !== null) {
    return this.ADMIN_USER_INFO;
  }
  let result = await this.getService(`/admin/role/getAdminInfo`, request);
  if (result.code === 0) {
    this.ADMIN_USER_INFO = result;
    store.commit('SET_USER', result.data || {});
    result = await Service.adminSystemGetSystemConfigNoToken();
    if (result.code === 0) {
      window.document.title = result.data.seoTitle;
      store.dispatch('SetSiteInfo', result.data)
    }
  }
  return result;
};

/**
 * 管理员登出
 * @param isLocal 是否为本地登出
 * @returns {Promise<*>}
 */
Service.adminRoleLogout = async function (isLocal = false) {
  if (isLocal === true) {
    this.ADMIN_USER_INFO = null;
    removeSaaSId();
    return removeToken();
  }
  const request = new RequestParams();
  const result = await this.getService(`/admin/role/logout`, request);
  if (result.code === 0) {
    this.ADMIN_USER_INFO = null;
    store.commit('SET_TOKEN', '');
    // store.commit('SET_ROLES', []);
    removeSaaSId();
    removeToken();
  }
  return result;
};

/**
 * 发送短信验证码
 * @param params
 * @returns {Promise.<*>}
 */
Service.adminSendSmsCode = async function (params = {}) {
  const request = new RequestParams();
  // 手机号
  request.put('mobile', params['mobile']);
  return await this.getService(`/admin/password/sendCode`, request);
};

/**
 * 验证短信验证码
 * @param params
 * @returns {Promise.<*>}
 */
Service.adminCheckSmsCode = async function (params = {}) {
  const request = new RequestParams();
  // 手机号
  request.put('mobile', params['mobile']);
  // 验证码
  request.put('code', params['code']);
  return await this.getService(`/admin/password/checkVerification`, request);
};

/**
 * 修改密码
 * @param params
 * @returns {Promise.<*>}
 */
Service.adminChangePassword = async function (params = {}) {
  const request = new RequestParams();
  // 手机号
  request.put('mobile', params['mobile']);
  // 密码
  request.put('password', params['password']);
  return await this.getService(`/admin/password/updatePassword`, request);
};

/**
 * 图片上传
 * @param params
 * @returns {Promise.<*>}
 */
Service.adminImageUpload = async function (params = {}) {
  const request = new RequestParams();
  //  文件流
  request.put('file', params['file']);
  // SaasId
  request.put('mallId', getSaaSId());
  console.log(request);
  // return await this.getService(`/admin/image/upload`, request);
};

/**
 * 获取历史上传图片列表
 * @returns {Promise.<*>}
 */
Service.adminImageGetHistoryList = async function (params) {
  const request = new RequestParams();
  // SaasId
  request.put('mallId', getSaaSId() || '');
  // 页数
  request.put('page', params['page'], 1);
  // 每页条数
  request.put('pageSize', params['pageSize'], 20);
  return await this.getService(`/admin/image/getHistoryList`, request);
};

/**
 * 删除历史上传图片
 * @returns {Promise.<*>}
 */
Service.adminImageDelHistory = async function (params) {
  const request = new RequestParams();
  // SaasId
  request.put('mallId', getSaaSId() || '');
  //  图片ID(多个用,分隔)
  request.put('ids', params['ids']).checkEmpty('图片ID');
  return await this.getService(`/admin/image/delHistory`, request);
};

/**
 * 文件上传
 * @param params
 * @returns {Promise.<*>}
 */
Service.adminFileUpload = async function (params = {}) {
  const request = new RequestParams();
  // SaasId
  request.put('mallId', getSaaSId() || '');
  //  文件流
  request.put('file', params['file']);
  return await this.getService(`/admin/file/upload`, request);
};

// 文件转base64
Service.fileToBase64 = function (file) {
  return new Promise(function (resolve) {
    const fileReader = new FileReader();
    if (file) {
      fileReader.readAsDataURL(file);
    }
    fileReader.onload = () => {
      const base64Str = fileReader.result;
      const params = {
        name: file.name,
        size: file.size,
        type: file.type,
        content: base64Str.split(',')[1]
      };
      resolve(JSON.stringify(params));
    };
  })
}

// =================小程序配置
/**
 * 获取小程序的页面路径列表
 * @returns {Promise.<*>}
 */
Service.applicationGetWxAppPagePath = async function () {
  const request = new RequestParams();
  return await this.getService(`/admin/attach/handOut/getWxPath`, request);
};

/**
 * 添加小程序的页面路径
 * @returns {Promise.<*>}
 */
Service.applicationAddWxAppPagePath = async function (params) {
  const request = new RequestParams();
  // 页面名称
  request.put('title', params['title']).checkEmpty('页面名称');
  // 页面路径
  request.put('path', params['path']).checkEmpty('用户协议');
  return await this.getService(`/admin/attach/handOut/saveWxPath`, request);
};

/**
 * 获取小程序配置信息
 * @returns {Promise.<*>}
 */
Service.applicationGetWechatAppConfig = async function () {
  const request = new RequestParams();
  return await this.getService(`/admin/system/getConfig/wxApp`, request);
};

/**
 * 保存小程序配置信息
 * @returns {Promise.<*>}
 */
Service.applicationSaveWechatAppConfig = async function (params) {
  const request = new RequestParams();
  // appId
  request.put('appId', params['appId']).checkEmpty('appId');
  // 小程序密钥secret
  request.put('appSecret', params['appSecret']).checkEmpty('小程序密钥secret');
  // 腾讯地图key
  request.put('TCMapKey', params['TCMapKey']).checkEmpty('腾讯地图key');
  // 商户号
  request.put('payMch', params['payMch']).checkEmpty('商户号');
  // 商户支付secret
  request.put('paySecret', params['paySecret']).checkEmpty('商户支付secret');
  // 证书
  request.put('certPem', params['certPem']).checkEmpty('证书');
  // Key证书
  request.put('keyPem', params['keyPem']).checkEmpty('Key证书');
  // 小程序上传密钥
  request.put('wxMiniUploadKey', params['wxMiniUploadKey']);
  // 服务商关联票据
  request.put('serviceProviderTicket', params['serviceProviderTicket']);
  return await this.getService(`/admin/system/saveConfig/wxApp`, request);
};

/**
 * 获取第三方授权码
 * @returns {Promise.<*>}
 */
Service.applicationGetWxOpenAuthorizationQRCode = async function (params) {
  const request = new RequestParams();
  // appId
  request.put('url', params['url'])
  return await this.getService(`/admin/wxOpen/getAuthorizationUrl`, request);
};

// =================小程序配置

// =================短信配置

/**
 * 获取短信配置信息
 * @returns {Promise.<*>}
 */
Service.applicationGetSMSConfig = async function () {
  const request = new RequestParams();
  return await this.getService(`/admin/system/getConfig/sms`, request);
};

/**
 * 保存短信配置信息
 * @returns {Promise.<*>}
 */
Service.applicationSaveSMSConfig = async function (params) {
  const request = new RequestParams();
  // 短信ID
  request.put('smsId', params['smsId']).checkEmpty('短信ID');
  // 短信密钥
  request.put('smsSecret', params['smsSecret']).checkEmpty('短信密钥');
  // 签名
  request.put('smsSign', params['smsSign']).checkEmpty('签名');
  // 支付成功短信通知状态
  request.put('payTplStatus', params['payTplStatus']);
  // 支付成功通知模板id
  request.put('payTpl', params['payTpl']);
  // 订单分配短信通知状态
  request.put('allocateTplStatus', params['allocateTplStatus']);
  // 分配模板id
  request.put('allocateTpl', params['allocateTpl']);
  // 退款短信通知状态
  request.put('refundTplStatus', params['refundTplStatus']);
  // 退款模板id
  request.put('refundTpl', params['refundTpl']);
  // 取消订单短信通知状态
  request.put('cancelTplStatus', params['cancelTplStatus']);
  // 取消模板id
  request.put('cancelTpl', params['cancelTpl']);
  // 司机接单短信通知状态
  request.put('acceptTplStatus', params['acceptTplStatus']);
  // 司机接单模板id
  request.put('acceptTpl', params['acceptTpl']);
  // 新订单提醒状态
  request.put('remindTplStatus', params['remindTplStatus']);
  // 新订单提醒id
  request.put('remindTpl', params['remindTpl']);
  // 司机接单提醒用户及管理员状态
  request.put('remindAcceptTplStatus', params['remindAcceptTplStatus']);
  // 司机接单提醒用户及管理员状态
  request.put('remindAcceptTpl', params['remindAcceptTpl']);
  //  订单完成
  request.put('finishTplStatus', params['finishTplStatus']);
  // 订单完成
  request.put('finishTpl', params['finishTpl']);
  return await this.getService(`/admin/system/saveConfig/sms`, request);
};

// =================短信配置

// =================模板消息配置

/**
 * 获取模板消息配置信息
 * @returns {Promise.<*>}
 */
Service.applicationGetWxMessageConfig = async function () {
  const request = new RequestParams();
  return await this.getService(`/admin/system/getConfig/wxMessage`, request);
};

/**
 * 保存模板消息配置信息
 * @returns {Promise.<*>}
 */
Service.applicationSaveWxMessageConfig = async function (params) {
  const request = new RequestParams();
  // 新用户注册
  request.put('registerTpl', params['registerTpl']);
  request.put('registerTplStatus', params['registerTplStatus']);
  // 支付通知
  request.put('payTpl', params['payTpl']);
  request.put('payTplStatus', params['payTplStatus']);
  // 接单通知
  request.put('receiptTpl', params['receiptTpl']);
  request.put('receiptTplStatus', params['receiptTplStatus']);
  // 退款通知
  request.put('refundTpl', params['refundTpl']);
  request.put('refundTplStatus', params['refundTplStatus']);
  // 订单未支付
  request.put('notPayTpl', params['notPayTpl']);
  request.put('notPayTplStatus', params['notPayTplStatus']);
  // 订单取消
  request.put('revokeTpl', params['revokeTpl']);
  request.put('revokeTplStatus', params['revokeTplStatus']);
  // 订单过期
  request.put('expireTpl', params['expireTpl']);
  request.put('expireTplStatus', params['expireTplStatus']);
  // 预约过期
  request.put('appointmentExpireTpl', params['appointmentExpireTpl']);
  request.put('appointmentExpireTplStatus', params['appointmentExpireTplStatus']);
  // 新订单提醒
  request.put('orderRemindTpl', params['orderRemindTpl']);
  request.put('orderRemindTplStatus', params['orderRemindTplStatus']);
  return await this.getService(`/admin/system/saveConfig/wxMessage`, request);
};

// =================模板消息配置

// =================关于我们配置

/**
 * 获取模板消息配置信息
 * @returns {Promise.<*>}
 */
Service.applicationGetAboutUsConfig = async function () {
  const request = new RequestParams();
  return await this.getService(`/admin/system/getConfig/aboutUs`, request);
};

/**
 * 保存模板消息配置信息
 * @returns {Promise.<*>}
 */
Service.applicationSaveAboutUsConfig = async function (params) {
  const request = new RequestParams();
  // 内容
  request.put('aboutUs', params['aboutUs']).checkEmpty('内容');
  return await this.getService(`/admin/system/saveConfig/aboutUs`, request);
};

// =================关于我们配置

// =================获取腾讯云配置信息
/**
 * 获取站点信息
 * @returns {Promise.<*>}
 */
Service.adminSystemGetTencentCloudConfig = async function () {
  const request = new RequestParams();
  return await this.getService(`/admin/system/getConfig/tc`, request);
};

/**
 * 更新站点信息
 * @returns {Promise.<*>}
 */
Service.adminSystemUpdateTencentCloudConfig = async function (params) {
  const request = new RequestParams();
  // 密钥id
  request.put('secretId', params['secretId']);
  // 密钥
  request.put('secretKey', params['secretKey']);
  // 存储桶
  request.put('bucket', params['bucket']);
  // 地域
  request.put('region', params['region']);
  return await this.getService(`/admin/system/saveConfig/tc`, request);
};
// =================获取腾讯云配置信息

/**
 * 获取站点信息
 * @returns {Promise.<*>}
 */
Service.adminSystemGetSiteInfo = async function () {
  const request = new RequestParams();
  return await this.getService(`/admin/system/getSiteInfo`, request);
};

/**
 * 免登录获取站点信息
 * @returns {Promise.<*>}
 */
Service.adminSystemGetSystemConfigNoToken = async function () {
  const request = new RequestParams();
  return await this.getService(`/admin/getConfig`, request);
};

/**
 * 获取站点信息
 * @returns {Promise.<*>}
 */
Service.adminSystemGetSystemConfig = async function () {
  const request = new RequestParams();
  return await this.getService(`/admin/system/getConfig/loginConf`, request);
};

/**
 * 更新站点信息
 * @returns {Promise.<*>}
 */
Service.adminSystemUpdateSystemConfig = async function (params) {
  const request = new RequestParams();
  // 站点名称
  request.put('siteTitle', params['siteTitle']).checkEmpty('站点名称');
  // 页面title
  request.put('seoTitle', params['seoTitle']).checkEmpty('页面title');
  // 版权信息
  request.put('copyright', params['copyright']);
  return await this.getService(`/admin/system/saveConfig/loginConf`, request);
};

/**
 * 获取省市区
 * @returns {Promise.<*>}
 */
Service.applicationGetProvinceList = async function () {
  const request = new RequestParams();
  return await this.getService(`/admin/attach/handOut/getProvince`, request);
};
